//
//  ViewController.m
//  UI02_UILabel_UITextField_UIImageView_UIButton
//
//  Created by Scott on 16/5/4.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "SimpleLabel.h"
#import "SimpleTextField.h"
#import "SimpleButton.h"
#import "SimpleImageView.h"


@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, retain) SimpleTextField *tfOfUser;
@property (nonatomic, retain) SimpleTextField *tfOfPasswd;
@property (nonatomic, retain) SimpleTextField *tfOfPasswdAgain;

@end

@implementation ViewController

- (void)dealloc {
    [_tfOfUser release];
    [_tfOfPasswd release];
    [_tfOfPasswdAgain release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createLabel];
    [self createTextField];
    [self createButton];
    [self createImageView];
}

#pragma mark - 知识点1: UILabel
- (void)createLabel {
    
    // 创建对象.
    SimpleLabel *labelOfUser = [[SimpleLabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    [self.view addSubview:labelOfUser];
    
    [labelOfUser release];
    
    labelOfUser.backgroundColor = [UIColor whiteColor];
    
    // API:
    labelOfUser.text = @"用户名:";
    
    // 阴影.
    labelOfUser.shadowColor = [UIColor redColor];
    
    labelOfUser.shadowOffset = CGSizeMake(5, 5);
    
    // 显示行数
    labelOfUser.numberOfLines = 0;
    
    // 对齐方式
    labelOfUser.textAlignment = NSTextAlignmentRight;
    
    
    // 密码Label
    SimpleLabel *labelOfPasswd = [SimpleLabel simpleLabelWithFrame:CGRectMake(20, 160, 100, 40) title:@"密码:"];
    [self.view addSubview:labelOfPasswd];
    
    
    // 再次确认密码
    SimpleLabel *labelOfPasswdAgain = [SimpleLabel simpleLabelWithFrame:CGRectMake(20, 220, 130, 40) title:@"再次输入密码:" superView:self.view];
    
    
}

#pragma mark - 知识点2: UITextField
- (void)createTextField {
    
    // 创建对象
    self.tfOfUser = [[SimpleTextField alloc] initWithFrame:CGRectMake(140, 100, 200, 40)];
    [self.view addSubview:self.tfOfUser];
    [_tfOfUser release];
    
    
    // 常用API
    self.tfOfUser.placeholder = @"请输入用户名";
    
    // 边框风格
    self.tfOfUser.borderStyle = UITextBorderStyleRoundedRect;
    
    // 密文输入
    self.tfOfUser.secureTextEntry = YES;
    
    self.tfOfUser.returnKeyType = UIReturnKeyNext;
    
    self.tfOfUser.keyboardType = UIKeyboardTypeDefault;
    
    self.tfOfUser.keyboardAppearance = UIKeyboardAppearanceDark;
    
    // 指定delegate
    self.tfOfUser.delegate = self;
    
    // clearButton
    self.tfOfUser.clearButtonMode = UITextFieldViewModeAlways;
    
    
    // 密码.
    self.tfOfPasswd = [[SimpleTextField alloc] initWithFrame:CGRectMake(140, 160, 200, 40)];
    [self.view addSubview:self.tfOfPasswd];
    [_tfOfPasswd release];
    
    self.tfOfPasswd.placeholder = @"请输入密码";
    self.tfOfPasswd.borderStyle = 3;
    
    self.tfOfPasswd.delegate = self;
    
    // 再次密码.
    self.tfOfPasswdAgain = [[SimpleTextField alloc] initWithFrame:CGRectMake(160, 220, 180, 40)];
    [self.view addSubview:self.tfOfPasswdAgain];
    [_tfOfPasswdAgain release];
    
    self.tfOfPasswdAgain.placeholder = @"请再次输入密码";
    self.tfOfPasswdAgain.borderStyle = 3;
    
    self.tfOfPasswdAgain.delegate = self;
}

#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    // 回收键盘.
    NSLog(@"%@", textField);
    
    // 释放第一响应, 系统的实现就是回收键盘.
    [textField resignFirstResponder];
    
    return YES;
}

/** 
 * @brief 文本框内容发生变化时调用此协议方法. 
 * 
 * @param textField 正在编辑的TextField对象.
 * @param range 改变字符的范围
 * @param string 替换的字符串
 *
 * @return BOOL 是否可以替换.
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSLog(@"%@", NSStringFromRange(range));
    NSLog(@"%@", string);
    
    // 作业: 查baidu , 看此法如何使用.
    
    return YES;
    
}

/** 点击 clear 按键时调用此方法. */
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    
    NSLog(@"%s", __FUNCTION__);
    
    return YES;
}

#pragma mark - 知识点3: UIButton
- (void)createButton {
    
    // 创建对象.
    SimpleButton *buttonOfLogin = [SimpleButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:buttonOfLogin];
    
    
    buttonOfLogin.frame = CGRectMake(40, 300, 100, 40);
    
    [buttonOfLogin setTitle:@"Login" forState:UIControlStateNormal];
    
    buttonOfLogin.backgroundColor= [UIColor redColor];
    
    // 添加点击事件.
    [buttonOfLogin addTarget:self action:@selector(handleLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    
    SimpleButton *buttonOfCancel = [SimpleButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:buttonOfCancel];
    
    
    buttonOfCancel.frame = CGRectMake(180, 300, 100, 40);
    
    buttonOfCancel.backgroundColor = [UIColor cyanColor];
    
    [buttonOfCancel setTitle:@"Cancel" forState:UIControlStateNormal];
    
    [buttonOfCancel addTarget:self action:@selector(handleCancel:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)handleCancel:(SimpleButton *)button {
    
    // 三个输入框清空.
    self.tfOfUser.text = @"";
    self.tfOfPasswd.text = @"";
    self.tfOfPasswdAgain.text = @"";
}
- (void)handleLogin:(SimpleButton *)button {
    
    // 验证用户名和密码是否正确.
    if ([self.tfOfPasswd.text isEqualToString:self.tfOfPasswdAgain.text]) {
        
        if ([self.tfOfPasswd.text isEqualToString:@"123456"] && [self.tfOfUser.text isEqualToString:@"Tom"]) {
            
            NSLog(@"登录成功!");
        
        } else {
            
            NSLog(@"用户不存在或密码错误");
        }
        
        
    } else {
    
        NSLog(@"两个密码不一致");
    }
    
    
}

#pragma mark - 知识点4: UIImageView
- (void)createImageView {
    
    // 创建对象
    UIImage *image = [UIImage imageNamed:@"qq.jpg"];
    
    SimpleImageView *imageViewOfBackground = [[SimpleImageView alloc] initWithImage:image];
    
    [self.view addSubview:imageViewOfBackground];
    [imageViewOfBackground release];
    
    
    imageViewOfBackground.frame = self.view.bounds;
    
    [self.view sendSubviewToBack:imageViewOfBackground];
}




@end
